package au.csiro.nt.psdp.acceptance;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 *
 */
public abstract class TradelabWebTestCase extends WebTestCaseWrapper
{
    private static final Logger log = Logger.getLogger(TradelabWebTestCase.class);
    private static final String DEFAULT_BASE_URL = "http://localhost:8080/tradelab";
    private static final String KEY_TRADELAB_URL = "tradelab.url";
    private static String baseUrl;

    protected void setUp() throws Exception
    {
        super.setUp();
        init();
        login();
    }

    private void init()
    {
        if (baseUrl == null)
        {
            baseUrl = DEFAULT_BASE_URL;
            try
            {
                InputStream propertiesStream = TradelabWebTestCase.class.getResourceAsStream("localtest.properties");
                if (propertiesStream == null)
                {
                    log.warn("Did not find localtest.properties file, using defaults.");
                }
                else
                {
                    Properties properties = new Properties();
                    properties.load(propertiesStream);
                    baseUrl = properties.getProperty(KEY_TRADELAB_URL, DEFAULT_BASE_URL);
                }
            }
            catch (IOException e)
            {
                log.warn("Error: could not parse localtest.properties");
            }
        }
        getTestContext().setBaseUrl(baseUrl);
        beginAt("/");
    }

    protected void login()
    {
        gotoPage("/login.jsp");
        setTextField("j_username", "jyzhang");
        setTextField("j_password", "tomcat");
        submit();
    }
}
